package aod.android.com.bernay.mine.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.message.adapter.PagerAdapter;
import aod.android.com.bernay.mine.fragment.AlbumFragment;
import aod.android.com.bernay.mine.fragment.VedioFragment;
import aod.android.com.bernay.mine.module.AlbumInnerBean;
import aod.android.com.bernay.util.MessageEvent;
import aod.android.com.bernay.util.Utils;
import chen.lion.hilib.view.NoScrollViewPager;
import chen.lion.hilib.view.bind.HiView;

/**
 * @author crated by zhixianuanyang on 2019/4/3 12:21
 * @Email jamesKing0812@gmail.com
 */
public class AlbumActivity extends BernayBaseActivity implements View.OnClickListener {

    @HiView(R.id.mViewPager)
    NoScrollViewPager mViewPager;

    @HiView(R.id.tb_layout)
    TabLayout mTabLayout;

    @HiView(R.id.title_iv_back)
    ImageView titleBack;

    @HiView(R.id.iv_editor)
    TextView mEditor;

    @HiView(R.id.iv_selectedAll)
    TextView mSelectedAll;

    @HiView(R.id.tv_cancel)
    TextView mCancel;

    @HiView(R.id.iv_selected)
    TextView mSelected;

    @HiView(R.id.ll_tablayout)
    LinearLayout layoutTablayout;

    @HiView(R.id.ll_selected)
    LinearLayout layoutSelected;

    private String selectNumber;
    private AlbumFragment albumFragment;
    private VedioFragment vedioFragment;

    private PagerAdapter pagerAdapter;
    private String[] titles = {"图片","视频"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int tabSelect = 1;

    @Override
    protected int initLayout() {
        return R.layout.activity_album;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initData() {
        initTablayout();
        mSelectedAll.setOnClickListener(this);
        mEditor.setOnClickListener(this);
        mCancel.setOnClickListener(this);
        titleBack.setOnClickListener(this);
    }

    private void initTablayout() {
        pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        albumFragment = new AlbumFragment();
        vedioFragment = new VedioFragment();
        mFragments.add(albumFragment);
        mFragments.add(vedioFragment);
        pagerAdapter.setTitles(titles);
        pagerAdapter.setFragments(mFragments);
        mViewPager.setAdapter(pagerAdapter);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setupWithViewPager(mViewPager);

        if(mTabLayout != null) {
            TabLayout.Tab tab1 = mTabLayout.getTabAt(0);
            if (tab1 != null) {
                tab1.setCustomView(Utils.getTabView0(AlbumActivity.this,titles[0]));
            }
            TabLayout.Tab tab2 = mTabLayout.getTabAt(1);
            if (tab2 != null) {
                tab2.setCustomView(Utils.getTabView1(AlbumActivity.this,titles[1]));
            }
        }

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if ("图片".equals(tab.getText().toString())){
                    tabSelect = 1;
                }else{
                    tabSelect = 2;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_iv_back:
                finish();
                break;
            case R.id.iv_editor:
                setEditor();
                break;
            case R.id.tv_cancel:
                setCancelData();
                break;
            case R.id.iv_selectedAll:
                albumFragment.initSelect();
                if (albumFragment.isSelectAll){
                    albumFragment.getListItems();
                    mSelected.setText("已选 "+albumFragment.getListSize() +" / 共"+albumFragment.getListSize());
                }else{
                    Utils.selectedList.clear();
                    mSelected.setText("已选 "+ Utils.selectedList.size() +" / 共"+albumFragment.getListSize());
                }
            break;
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        mSelected.setText("已选 "+ Utils.selectedList.size() +" / 共"+albumFragment.getListSize());
    }


    //设置选中参数
    private void setEditor(){
        albumFragment.controlCheck(true);
        mSelected.setText("已选 "+Utils.selectedList.size()+" / 共"+albumFragment.getListSize());
        mEditor.setVisibility(View.GONE);
        titleBack.setVisibility(View.GONE);
        mCancel.setVisibility(View.VISIBLE);
        layoutTablayout.setVisibility(View.GONE);
        layoutSelected.setVisibility(View.VISIBLE);
        mSelectedAll.setVisibility(View.VISIBLE);
        albumFragment.unSelected();
    }

    /**
     * 设置取消参数
     */
    private void setCancelData(){
        Utils.selectedList.clear();
        albumFragment.unSelected();
        albumFragment.controlCheck(false);
        mCancel.setVisibility(View.GONE);
        mEditor.setVisibility(View.VISIBLE);
        titleBack.setVisibility(View.VISIBLE);
        mSelectedAll.setVisibility(View.GONE);
        layoutSelected.setVisibility(View.GONE);
        layoutTablayout.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
